<?php
namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

require_once 'resources/org/code/Code.class.php';
class LoginController extends CommonController{
    public function index(){
        if($input = Input::all()){
            $code = new \Code();
            $code_num= $code->get();
            if(strtoupper($code_num) != strtoupper($input['code'])){
                return back()->with('msg','验证码错误');
            }
            $user = User::where('user_name',$input['user_name'])->first();
            if(empty($user)){
                return back()->with('msg','用户名不存在');
            }else{
                if(Crypt::decrypt($user['password']) != $input['password']){
                    return back()->with('msg','密码错误');
                }
            }
            session(['user'=>$user]);
            return redirect('admin/index');
        }

        return view('admin/login');
    }
    public function code(){

        $code = new \Code();
        $code->make();
    }
    public function getcode(){

        $code = new \Code();
        echo $code->get();
    }
    public function crypt(){
//        $user = DB::select('select * from blog_user where user_id = :idss',['idss'=>1]);
//        $user= DB::table('blog_category')->lists('cate_name','cate_id');
//        $user = DB::table('blog_category')->select('cate_name', 'cate_title','cate_pid')->get();
//        $query = DB::table('blog_category')->select('cate_name');
//        $user = $query->addSelect('cate_view')->get();
        $user = DB::table('blog_user')
            ->join('blog_category', 'blog_user.user_id', '=', 'blog_category.cate_id')
            ->select('blog_user.*', 'blog_category.cate_id', 'blog_category.cate_name')
            ->get();
        dd($user);


    }
    public function loginout(){
        session(['user'=>null]);
        return redirect('admin/login');
    }
}